<template>
  <template v-if="getShow">
    <LoginFormTitle class="enter-x" />
    <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef">
      <!-- tenantCode select -->
      <FormItem name="tenantCode" class="enter-x">
        <Select
          v-model:value="formData.tenantCode"
          size="large"
          :options="tenantCodeOptions"
          placeholder="机构"
        />
      </FormItem>
      <FormItem name="identifier" class="enter-x">
        <Input
          class="fix-auto-fill"
          size="large"
          v-model:value="formData.identifier"
          :placeholder="t('sys.login.userName')"
        />
      </FormItem>
      <FormItem name="password" class="enter-x">
        <StrengthMeter
          size="large"
          v-model:value="formData.password"
          :placeholder="t('sys.login.password')"
        />
      </FormItem>
      <FormItem name="confirmPassword" class="enter-x">
        <InputPassword
          size="large"
          visibilityToggle
          v-model:value="formData.confirmPassword"
          :placeholder="t('sys.login.confirmPassword')"
        />
      </FormItem>

      <Button
        type="primary"
        class="enter-x"
        size="large"
        block
        @click="handleRegister"
        :loading="loading"
      >
        {{ t('sys.login.registerButton') }}
      </Button>
      <Button size="large" block class="mt-4 enter-x" @click="handleBackLogin">
        {{ t('sys.login.backSignIn') }}
      </Button>
    </Form>
  </template>
</template>
<script lang="ts" setup>
  import { reactive, ref, unref, computed } from 'vue';
  import LoginFormTitle from './LoginFormTitle.vue';
  import { Form, Input, Button, message, Select } from 'ant-design-vue';
  import { StrengthMeter } from '/@/components/StrengthMeter';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useUserStore } from '/@/store/modules/user';
  import { getTenantList } from '/@/api/sys/tenant';

  import { useLoginState, useFormRules, useFormValid, LoginStateEnum } from './useLogin';
  const FormItem = Form.Item;
  const InputPassword = Input.Password;
  const { t } = useI18n();
  const { handleBackLogin, getLoginState } = useLoginState();

  const formRef = ref();
  const loading = ref(false);

  const formData = reactive({
    identifier: '',
    password: '',
    confirmPassword: '',
    mobile: '',
    sms: '',
    policy: false,
    tenantCode: 'hyy',
  });

  const { getFormRules } = useFormRules(formData);
  const { validForm } = useFormValid(formRef);
  const userStore = useUserStore();

  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.REGISTER);

  async function handleRegister() {
    const data = await validForm();
    if (!data) {
      return;
    }

    loading.value = true;
    const res = await userStore.register({
      password: data.password,
      identifier: data.identifier,
      tenantCode: data.tenantCode,
      mode: 'none',
      sceneStr: '',
    });
    if (res) {
      message.success('注册成功');
      handleBackLogin();
    }
  }

  // 查询机构信息
  const tenantCodeOptions = ref<any[]>([]);
  async function getTenantDataList() {
    // 查询全部
    const res = await getTenantList({
      pageSize: 1000,
      pageNum: 1,
      tenantCode: formData.tenantCode,
    });
    if (res) {
      tenantCodeOptions.value = res.list.map((item) => {
        return { label: item.tenantName, value: item.tenantCode };
      });
    }
  }

  getTenantDataList();
</script>
